package com.atguigu.flink.chapter11.function;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/23 11:06
 */
public class Flink04_Function_TableAgg {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        DataStreamSource<WaterSensor> stream =
            env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_1", 4000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 3000L, 30),
                             new WaterSensor("sensor_2", 6000L, 60));
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Table table = tEnv.fromDataStream(stream);
        tEnv.createTemporaryView("sensor", table);
        
        table
            .groupBy($("id"))
            // 使用制表聚合函数
            .flatAggregate(call(MyTop2.class, $("vc")))
            .select($("id"), $("rank"), $("vc"))
            .execute()
            .print();
        
        // 目前sql语句中不能直接用
        
    }
    
    public static class MyTop2 extends TableAggregateFunction<Result, FirstSecond> {
        
        // 初始化累计器
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 对数据进行累加
        public void accumulate(FirstSecond fs, Integer vc) {
            if (vc > fs.first) {
                fs.second = fs.first;
                fs.first = vc;
            } else if (vc > fs.second) {
                fs.second = vc;
            }
        }
        
        // 约定的方法. 用来向流中输出最终结果
        public void emitValue(FirstSecond fs, Collector<Result> out) {
            out.collect(new Result("第一名", fs.first));
            if (fs.second > 0) {
                out.collect(new Result("第二名", fs.second));
            }
        }
        
    }
    
    // 累加器
    public static class FirstSecond {
        
        public Integer first = 0;
        public Integer second = 0;
    }
    
    public static class Result {
        
        public Result(String rank, Integer vc) {
            this.rank = rank;
            this.vc = vc;
        }
        
        public String rank;
        public Integer vc;
    }
    
}
/*
new WaterSensor("sensor_1", 1000L, 10)
每来一条数据, 输出两行:
    第一名    10
    第二名    5

有没有聚合? 有
有制表?  一行变两行 有

 */